package com.gimi.cloud.jimi.manage.consumer.service.sys.impl;


import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.exception.MsgException;
import com.gimi.cloud.jimi.common.utils.EntityVerification;
import com.gimi.cloud.jimi.common.utils.JsonUtil;
import com.gimi.cloud.jimi.common.utils.reqhead.Response;
import com.gimi.cloud.jimi.common.utils.reqhead.Util;
import com.gimi.cloud.jimi.common.utils.reqhead.Verification;
import com.gimi.cloud.jimi.dto.params.*;
import com.gimi.cloud.jimi.dto.sys.SysMasterDTO;
import com.gimi.cloud.jimi.manage.consumer.feign.SystemFeignService;
import com.gimi.cloud.jimi.manage.consumer.service.sys.MasterService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by shazhiwei on 2017/12/14.
 */
@Service
public class MasterServiceImpl implements MasterService{
    private final SystemFeignService masterFeignService;
    private static Logger logger = LoggerFactory.getLogger(MasterServiceImpl.class);

    public MasterServiceImpl(SystemFeignService masterFeignService){
        this.masterFeignService = masterFeignService;
    }

    @Override
    public Response addMaster(@RequestBody String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        AddMasterParams params = JsonUtil.jsonToBean(requestBody,AddMasterParams.class);
        Verification verification = EntityVerification.checkEntity(params);
        if(!verification.getCheckResult()){
            Response resp = new Response();
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR,verification.getMsg());
            return resp;
        }else{
            return masterFeignService.addMaster(params);
        }
    }

    @Override
    public Response editMaster(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        AddMasterParams params = JsonUtil.jsonToBean(requestBody,AddMasterParams.class);
        Verification verification = EntityVerification.checkEntity(params);
        if(!verification.getCheckResult()){
            Response resp = new Response();
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR,verification.getMsg());
            return resp;
        }else{
            return masterFeignService.editMaster(params);
        }
    }

    @Override
    public Response deleteMaster(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));

        DeleteMasterParams params = JsonUtil.jsonToBean(requestBody,DeleteMasterParams.class);
        Verification verification = EntityVerification.checkEntity(params);
        if(!verification.getCheckResult()){
            Response resp = new Response();
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR,verification.getMsg());
            return resp;
        }else{
            return masterFeignService.deleteMaster(params);
        }
    }

    @Override
    public Response login(@RequestBody String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        LoginParams params = JsonUtil.jsonToBean(requestBody,LoginParams.class);
        Verification verification = EntityVerification.checkEntity(params);
        if(!verification.getCheckResult()){
            Response resp = new Response();
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR,verification.getMsg());
            return resp;
        }else{
            return masterFeignService.login(params);
        }
    }

    @Override
    public Response loginOut(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        SysMasterDTO masterDTO = JsonUtil.jsonToBean(requestBody,SysMasterDTO.class);
        return masterFeignService.loginOut(masterDTO);
    }

    @Override
    public Response getMasterList(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        MasterListParams params = JsonUtil.jsonToBean(requestBody,MasterListParams.class);
        Verification verification = EntityVerification.checkEntity(params);
        if(!verification.getCheckResult()){
            Response resp = new Response();
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR,verification.getMsg());
            return resp;
        }else{
            return masterFeignService.getList(params);
        }
    }

    @Override
    public Response getMasterSingle(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        MasterSingleParams params = JsonUtil.jsonToBean(requestBody,MasterSingleParams.class);
        Verification verification = EntityVerification.checkEntity(params);
        if(!verification.getCheckResult()){
            Response resp = new Response();
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR,verification.getMsg());
            return resp;
        }else{
            return masterFeignService.getMasterSingle(params);
        }
    }

}
